﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SoundEdge.Media.Wave;
using SoundEdge.Media.Settings;

namespace SoundEdge.WinControls.Settings
{
    public partial class WaveOutSettingsPanel : UserControl
    {
        public int SelectedDeviceNumber { get { return comboBoxWaveOutDevice.SelectedIndex; } }
        public WaveCallbackStrategy CallbackStrategy { get { return ((CallbackComboItem)comboBoxCallback.SelectedItem).Strategy; } }

        private WaveOutSettings _settings;

        public WaveOutSettingsPanel(WaveOutSettings settings)
        {
            _settings = settings;
            InitializeComponent();
            InitialiseDeviceCombo();
            InitialiseStrategyCombo();
        }

        class CallbackComboItem
        {
            public CallbackComboItem(string text, WaveCallbackStrategy strategy)
            {
                this.Text = text;
                this.Strategy = strategy;
            }
            public string Text { get; private set; }
            public WaveCallbackStrategy Strategy { get; private set; }
        }

        private void InitialiseDeviceCombo()
        {
            for (int deviceId = 0; deviceId < WaveOut.DeviceCount; deviceId++)
            {
                var capabilities = WaveOut.GetCapabilities(deviceId);
                comboBoxWaveOutDevice.Items.Add(String.Format("Device {0} ({1})", deviceId, capabilities.ProductName));
            }
            if (comboBoxWaveOutDevice.Items.Count > 0)
            {
                comboBoxWaveOutDevice.SelectedIndex = _settings == null || _settings.DeviceNumber > comboBoxWaveOutDevice.Items.Count - 1 ? 0 : _settings.DeviceNumber;
            }
        }

        private void InitialiseStrategyCombo()
        {
            comboBoxCallback.DisplayMember = "Text";
            comboBoxCallback.ValueMember = "Strategy";
            comboBoxCallback.Items.Add(new CallbackComboItem("Window", WaveCallbackStrategy.NewWindow));
            comboBoxCallback.Items.Add(new CallbackComboItem("Function", WaveCallbackStrategy.FunctionCallback));
            comboBoxCallback.Items.Add(new CallbackComboItem("Event", WaveCallbackStrategy.Event));
            CallbackComboItem selected = null;
            if (_settings != null)
            {
                selected = comboBoxCallback.Items.OfType<CallbackComboItem>().FirstOrDefault(item => item.Strategy == _settings.CallbackStrategy);
            }

            comboBoxCallback.SelectedIndex = selected == null ? 0 : comboBoxCallback.Items.IndexOf(selected);
        }

        private void comboBoxCallback_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxCallback.SelectedItem != null)
            {
                _settings.CallbackStrategy = (comboBoxCallback.SelectedItem as CallbackComboItem).Strategy;
            }
        }

        private void comboBoxWaveOutDevice_SelectedIndexChanged(object sender, EventArgs e)
        {
            _settings.DeviceNumber = comboBoxWaveOutDevice.SelectedIndex;
        }


    }
}
